package modelUtils.gui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import modelUtils.CodeGenerateUtils;

public class GeneratorGui extends JFrame implements ActionListener{

	/** serialVersionUID*/
	private static final long serialVersionUID = -6617483149462053236L;
	// 定义组件
    JPanel jpInput, jpButton;
    //, jpDbUser, jpDbPassword, jpDiskPath, jpAuthor,
    //	jpCommonPackage, jpPackageName, jpModelPackageName, jpTableName, jpEntityPrefix, jpTableComment;
    JLabel jlDbDriver, jlDbUrl, jlDbUser, jlDbPassword, jlDiskPath, jlAuthor,
	jlCommonPackage, jlPackageName, jlModelPackageName, jlTableName, jlEntityPrefix, jlTableComment,jlIgnorColumn,jlIgnorColumnRemark, jlReadMe;
    // 按钮
    JButton jbSure, jbConcle,jbSelectPath;
    // 输入框
    JTextField jtDbDriver, jtDbUrl, jtDbUser, jtDbPassword, jtDiskPath, jtAuthor,
	jtCommonPackage, jtPackageName, jtModelPackageName, jtTableName, jtEntityPrefix, jtTableComment,jtIgnorColumn;
	
	public GeneratorGui(){
		// 标题栏  
		super("代码生成工具");
		// 大小
		setSize(800, 700);
		// 居中  
        setLocationRelativeTo(null);
        // 默认关闭操作  
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        // 初始化文本框与按钮  
        generateInterface();  
        // 显示  
        setVisible(true); 
        //setLayout(new GridLayout(3, 1));
	}
	
	 /** 
     * 初始化文本框与按钮，生成界面 
     */  
	public void generateInterface() {
		jpInput = new JPanel();
		jpInput.setLayout(null);
		jpButton = new JPanel();
		jpButton.setLayout(new FlowLayout());
		
		
		jlDbDriver = new JLabel("数据库驱动");
		jlDbDriver.setBounds(50, 20, 100, 25);
		
		jlDbUrl = new JLabel("数据库链接");
		jlDbUrl.setBounds(50, 60, 100, 25);
		
		jlDbUser = new JLabel("数据库用户名");
		jlDbUser.setBounds(50, 100, 100, 25);
		
		jlDbPassword = new JLabel("数据库密码");
		jlDbPassword.setBounds(50, 140, 100, 25);
		
		jlDiskPath = new JLabel("生成代码路径");
		jlDiskPath.setBounds(50, 180, 100, 25);
		
		jlAuthor = new JLabel("代码作者");
		jlAuthor.setBounds(50, 220, 100, 25);
		
		jlCommonPackage = new JLabel("公共类包");
		jlCommonPackage.setBounds(50, 260, 100, 25);
		
		jlPackageName = new JLabel("生成代码包");
		jlPackageName.setBounds(50, 300, 100, 25);
		
		jlModelPackageName = new JLabel("业务/模块包名");
		jlModelPackageName.setBounds(50, 340, 100, 25);
		
		jlTableName = new JLabel("数据库表名称");
		jlTableName.setBounds(50, 380, 100, 25);
		
		jlEntityPrefix = new JLabel("数据库表前缀");
		jlEntityPrefix.setBounds(50,420, 100, 25);
		
		jlTableComment = new JLabel("表注释");
		jlTableComment.setBounds(50, 460, 100, 25);
		
		jlIgnorColumn = new JLabel("实体类忽略字段");
		jlIgnorColumn.setBounds(50, 500, 100, 25);
		
		jlReadMe = new JLabel("*注意：使用该工具必须先创建数据库表，并且数据库表以及字段添加对应的注释！");
		jlReadMe.setBounds(50, 580, 550, 25);
		jlReadMe.setForeground(Color.red);
		
		jtDbDriver = new JTextField("com.mysql.jdbc.Driver");
		jtDbDriver.setBounds(140, 20, 240, 25);
		
		jtDbUrl = new JTextField("jdbc:mysql://127.0.0.1:3306/lawbzoffice20181218?autoReconnect=true&useUnicode=true&characterEncoding=utf8");
		jtDbUrl.setBounds(140, 60, 550, 25);
		
		jtDbUser = new JTextField("root");
		jtDbUser.setBounds(140, 100, 240, 25);
		
		jtDbPassword = new JTextField("1234");
		jtDbPassword.setBounds(140, 140, 240, 25);
		
		jtDiskPath = new JTextField("D:/src");
		jtDiskPath.setBounds(140, 180, 240, 25);
		jbSelectPath = new JButton("选择目录");
		jbSelectPath.setBounds(385, 180, 90, 25);
		jbSelectPath.addActionListener(this);
		
		
		jtAuthor = new JTextField("zhangxz");
		jtAuthor.setBounds(140, 220, 240, 25);
		
		jtCommonPackage = new JTextField("com.lingnet.common");
		jtCommonPackage.setBounds(140, 260, 240, 25);
		
		jtPackageName = new JTextField("com.lingnet.lawe");
		jtPackageName.setBounds(140, 300, 240, 25);
		
		jtModelPackageName = new JTextField("generalprogram");
		jtModelPackageName.setBounds(140, 340, 240, 25);
		
		jtTableName = new JTextField("lawe_form");
		jtTableName.setBounds(140, 380, 240, 25);
		
		jtEntityPrefix = new JTextField("lawe");
		jtEntityPrefix.setBounds(140, 420, 240, 25);
		
		jtTableComment = new JTextField("案件文书列表");
		jtTableComment.setBounds(140, 460, 240, 25);
		
		// 忽略字段
		jtIgnorColumn = new JTextField("id,createdate,modifydate");
		jtIgnorColumn.setBounds(140, 500, 300, 25);
		jlIgnorColumnRemark = new JLabel("*忽略字段不参与生成实体类；小写,多个字段英文逗号‘,’分开");
		jlIgnorColumnRemark.setBounds(50, 530, 600, 25);
		
		
		
		// 加入各个组件
		jpInput.add(jlDbDriver);
		jpInput.add(jtDbDriver);
		
		jpInput.add(jlDbUrl);
		jpInput.add(jtDbUrl);
		
		jpInput.add(jlDbUser);
		jpInput.add(jtDbUser);
		
		
		jpInput.add(jlDbPassword);
		jpInput.add(jtDbPassword);
		
		jpInput.add(jlDiskPath);
		jpInput.add(jtDiskPath);
		jpInput.add(jbSelectPath);
		
		
		jpInput.add(jlAuthor);
		jpInput.add(jtAuthor);
		
		jpInput.add(jlCommonPackage);
		jpInput.add(jtCommonPackage);
		
		jpInput.add(jlPackageName);
		jpInput.add(jtPackageName);
		
		jpInput.add(jlModelPackageName);
		jpInput.add(jtModelPackageName);
		
		jpInput.add(jlTableName);
		jpInput.add(jtTableName);
		
		jpInput.add(jlEntityPrefix);
		jpInput.add(jtEntityPrefix);
		
		jpInput.add(jlTableComment);
		jpInput.add(jtTableComment);
		
		jpInput.add(jlIgnorColumn);
		jpInput.add(jtIgnorColumn);
		jpInput.add(jlIgnorColumnRemark);
		
		jpInput.add(jlReadMe);
		
		
		
		jbSure = new JButton("生成代码");
		jbSure.addActionListener(this);
		jbConcle = new JButton("关闭");
		jbConcle.addActionListener(this);
		
		jpButton.add(jbSure);
		jpButton.add(jbConcle);
		
		
		this.add(jpInput,"Center");// 数据库驱动
		this.add(jpButton,"South");// 数据库链接
	}



	public GeneratorGui(GraphicsConfiguration gc) {
		super(gc);
		// TODO Auto-generated constructor stub
	}



	public GeneratorGui(String title, GraphicsConfiguration gc) {
		super(title, gc);
		// TODO Auto-generated constructor stub
	}



	public GeneratorGui(String title) throws HeadlessException {
		super(title);
		// TODO Auto-generated constructor stub
	}



	@Override
	public void actionPerformed(ActionEvent e) {
		//获取产生事件的事件源强制转换
        JButton bt = (JButton)e.getSource();
        //获取按钮上显示的文本
        String btnStr = bt.getText();
        if("选择目录".equals(btnStr)) {
        	JFileChooser chooser = new JFileChooser();
//        	chooser.setCurrentDirectory(new java.io.File("."));
        	chooser.setDialogTitle("选择生成代码路径");
        	chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        	chooser.setAcceptAllFileFilterUsed(false);
        	if (chooser.showOpenDialog(jpInput) == JFileChooser.APPROVE_OPTION) {
        		 jtDiskPath.setText(chooser.getSelectedFile().getAbsolutePath());
//        	     System.out.println("getCurrentDirectory(): "
//        	       + chooser.getCurrentDirectory());
//        	     System.out.println("getSelectedFile() : "
//        	       + chooser.getSelectedFile());
        	    } else {
        	     System.out.println("No Selection ");
        	    }
        	
        	
        }else if ("生成代码".equals(btnStr)) {
        	Map<String, String> data = new HashMap<>();
        	
        	String strDbDriver = jtDbDriver.getText().trim();
        	String strDbUrl = jtDbUrl.getText().trim(); 
        	String strDbUser = jtDbUser.getText().trim();
        	String strDbPassword = jtDbPassword.getText().trim();
        	String strDiskPath = jtDiskPath.getText().trim();
        	String strAuthor = jtAuthor.getText().trim();
        	String strCommonPackage = jtCommonPackage.getText().trim();
        	String strPackageName = jtPackageName.getText().trim();
        	String strModelPackageName = jtModelPackageName.getText().trim();
        	String strTableName = jtTableName.getText().trim();
        	String strEntityPrefix = jtEntityPrefix.getText().trim();
        	String strTableComment = jtTableComment.getText().trim();
        	String strIgnorColumn = jtIgnorColumn.getText().trim();
        	
        	data.put("driver", strDbDriver);
        	data.put("url", strDbUrl);
        	data.put("user", strDbUser);
        	data.put("password", strDbPassword);
        	data.put("disk_path", strDiskPath);
        	data.put("author", strAuthor);
        	data.put("commen_page", strCommonPackage);
        	data.put("package_name", strPackageName);
        	data.put("module_package_name", strModelPackageName);
        	data.put("table_name", strTableName);
        	data.put("entity_prefix", strEntityPrefix);
        	data.put("table_comment", strTableComment);
        	data.put("ignore_column", strIgnorColumn);
        	
        	boolean flag = CodeGenerateUtils.generateCode(data);
        	if (flag) {
        		JOptionPane.showMessageDialog(null, "代码生成成功！", "成功",JOptionPane.INFORMATION_MESSAGE);
			}else {
				JOptionPane.showMessageDialog(null, "生成代码失败，请检查输入信息！", "错误",JOptionPane.ERROR_MESSAGE);
			}
        	
			
		}else if("关闭".equals(btnStr)) {
			System.exit(0);
		}
		
	}

}
